package com.horizon.common.configuration;

import com.horizon.common.interceptor.LogInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 操作日志拦截器
 *
 * @author wangfuwei
 * @version 2018-08-03
 */
@Slf4j
@Order(0)
@SpringBootConfiguration
public class LoggerConfiguration implements WebMvcConfigurer {

    @Autowired
    private Environment environment;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (Boolean.valueOf(environment.getProperty("horizon.log.http", "true"))) {
            String staticPath = environment.getProperty("spring.mvc.static-path-pattern");
            InterceptorRegistration interceptorRegistration = registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**");

            //忽略拦截错误页面
            interceptorRegistration.excludePathPatterns("/error");

            //忽略拦截静态资源
            if (!StringUtils.isEmpty(staticPath) && !staticPath.equals("/**")) {
                interceptorRegistration.excludePathPatterns(staticPath);
            }
        }
    }
}
